package awd.bj.jls.webs.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import awd.bj.jls.webs.api.DictionaryApi;
import awd.bj.jls.webs.api.JlsServersApi;
import awd.bj.jls.webs.utils.PagerResult;
import awd.bj.jls.webs.utils.QueryParam;
import awd.bj.jls.webs.utils.Result;
import awd.bj.jls.webs.utils.Sort;
import awd.bj.jls.webs.utils.StringUtils;
import awd.bj.jls.webs.utils.TermType;
import awd.bj.manager.model.DictionaryModel;


/**
 * 字典服务
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/jls_dictionary")
@RefreshScope
public class DictionaryController {
    @Autowired
    private DictionaryApi dictionaryService;
    @Autowired
    private JlsServersApi jlsServersApi;

    /**
     * 下拉框数据获取
     *
     * @param node
     * @return
     */
    @PostMapping("/getDictionary")
    @ResponseBody
    @Cacheable(cacheNames = "web_cache_dictionary", key = "'web_cache_dictionary:jls:'+#node")
    public List<DictionaryModel> getDictionary(@RequestParam(value = "node", required = false, defaultValue = "NULL") String node) {
        try {
            return dictionaryService.getByField(node).getResult();
        } catch (Exception e) {
            // TODO: handle exception
            return null;
        }
    }

    /**
     * 下拉框数据分页获取
     *
     * @param request
     * @return
     */
    @PostMapping("/getDictionaryPage")
    @ResponseBody
//    @Cacheable(cacheNames = "web_cache_dictionary", key = "'web_cache_dictionary:page:'+" +
//            "#request.getParameter(\"pageIndex\")+" +
//            "#request.getParameter(\"pageSize\")+" +
//            "#request.getParameter(\"queryParams\")+" +
//            "#request.getParameter(\"fieldname\")+" +
//            "#request.getParameter(\"order\")+" +
//            "#request.getParameter(\"sort\")")
    public PagerResult<DictionaryModel> getDictionaryByPage(HttpServletRequest request) {
        QueryParam qParam = new QueryParam();
        String queryParams = request.getParameter("queryParams");
        String pageIndex = request.getParameter("pageIndex");
        String pageSize = request.getParameter("pageSize");
        if (StringUtils.isNotBlank(queryParams)) {

            if (StringUtils.IsValidInteger(queryParams)) {
                String name = "code";
                qParam.and(name,TermType.like,queryParams+"%");
            }
            if (StringUtils.containsCN(queryParams)) {
                String name = "content";
                qParam.and(name,TermType.like,"%"+queryParams+"%");

            }
            if (StringUtils.isEnglishChars(queryParams)) {
                String name = "py";
                char[] chars = queryParams.toCharArray();
                for (int i = 0; i < chars.length; i++) {
                    chars[i] = Character.toUpperCase(chars[i]);
                }
                qParam.and(name,TermType.like,new String(chars)+"%");
            }
        }
        
        
        String fieldname = request.getParameter("fieldname");
        if(StringUtils.isNotBlank(fieldname)) {
        	if(fieldname.equals("HJD")||fieldname.equals("JG")) {
        		fieldname = "XZQH";
        	}else if(fieldname.equals("CALX")) {
        		fieldname = "AJLB";
        	}else if(fieldname.equals("ZRDW")) {
        		fieldname = "BADW";
        	}else if(fieldname.equals("LSDW1")) {
        		fieldname = "LSDW";
        	}
        	qParam.and("fieldname", TermType.eq , fieldname);
        }
        if(StringUtils.isNotBlank(pageIndex)) {
        	qParam.setPageIndex(Integer.parseInt(pageIndex)-1);
        }
        if(StringUtils.isNotBlank(pageSize)) {
        	qParam.setPageSize(Integer.parseInt(pageSize));
        }
        String sortName = request.getParameter("sort");
        if (sortName != null && sortName.indexOf("String") > 0) {
        	sortName = sortName.substring(0, sortName.length() - 6);
        }
        String orderBy = request.getParameter("order");
        List<Sort> sorts = new ArrayList<>();
        Sort syplsort = new Sort();
        syplsort.setName("sypl");
        syplsort.setOrder("desc");
        Sort sort = new Sort();
        if(sortName != null && orderBy != null) {
        	sort.setName(sortName);
        	sort.setOrder(orderBy);
        }else {
        	sort.setName("code");
        	sort.setOrder("asc");
		}
        sorts.add(syplsort);
        sorts.add(sort);
        qParam.setSorts(sorts);
        try {
            PagerResult<DictionaryModel> pagerResult = dictionaryService.getPageByfields(qParam).getResult();
            return pagerResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new PagerResult<DictionaryModel>();
        }
    }


    /**
     * @Description: 根据code和fieldname来获取content
     * @Param: [code, fieldname]
     */
    @RequestMapping(value = "/getDictionaryContent", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result<?> getDictionaryContent(@RequestParam(value = "code", required = true) String code, @RequestParam(value = "fieldname", required = true) String fieldname) {
        return jlsServersApi.getDictionaryContent(fieldname, code);
    }

}
